﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaApplicationDemo.ViewModels;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using SukiUI;
using SukiUI.Controls;
using SukiUI.Dialogs;
using SukiUI.Toasts;

namespace AvaloniaApplicationDemo.ViewModels;

public partial class MainWindowViewModel : ObservableObject
{
    [ObservableProperty] private bool _isDarkTheme = true;
    [ObservableProperty] private SukiSideMenuItem _selectedMenuItem;
    public ISukiDialogManager DialogManager { get; }

    public ISukiToastManager ToastManager { get; }

    /// <summary>
    /// 菜单项集合
    /// </summary>
    public ObservableCollection<SukiSideMenuItem> MenuItems { get; set; }

    public MainWindowViewModel(
        HomePageViewModel homePageViewModel,
        AutoTestPageViewModel autoTestPageViewModel,
        ISukiDialogManager dialogManager,
        ISukiToastManager toastManager,
        BatteryTestViewModel batteryTestViewModel,
        ModbusConfigViewModel modbusConfigViewModel
    )
    {
        DialogManager = dialogManager;
        ToastManager = toastManager;
        MenuItems = new ObservableCollection<SukiSideMenuItem>
        {
            new()
            {
                Header = "首页",
                Icon = new PathIcon
                {
                    Data = Geometry.Parse(
                        "M505.5488 23.9104l-447.5904 376.32a85.0432 85.0432 0 0 0 54.784 150.1696h76.8l3.7376 0.256c4.096 0.512 9.0112 6.0416 9.0112 12.544v326.144a122.2144 122.2144 0 0 0 122.0096 121.856h95.5904A89.6 89.6 0 0 0 509.4912 921.6v-179.2c0-31.3856 17.92-53.76 35.328-53.76h36.864c17.408 0 35.328 22.3744 35.328 53.76V921.6c0 49.4592 40.0896 89.6 89.6 89.6h95.5392a121.9584 121.9584 0 0 0 122.0608-121.856V563.2a12.8 12.8 0 0 1 12.8-12.8h76.8a85.0432 85.0432 0 0 0 54.6816-150.1696L620.8512 23.9104a89.6 89.6 0 0 0-115.3024 0z m65.8944 58.7776l447.6416 376.32a8.2432 8.2432 0 0 1-5.2736 14.592h-76.8A89.6 89.6 0 0 0 847.36 563.2v326.144c0 24.832-20.2752 45.056-45.2608 45.056H706.56a12.8 12.8 0 0 1-12.8-12.8v-179.2c0-70.5024-48.128-130.56-112.128-130.56h-36.864c-64 0-112.128 60.0576-112.128 130.56V921.6a12.8 12.8 0 0 1-12.8 12.8H324.352a45.3632 45.3632 0 0 1-45.2608-45.2608V563.2A89.6 89.6 0 0 0 199.936 474.112l-8.192-0.512L112.64 473.6a8.2432 8.2432 0 0 1-5.3248-14.592l447.5904-376.32a12.8 12.8 0 0 1 16.4864 0z"),
                    Width = 24,
                    Height = 24
                },

                PageContent = homePageViewModel
            },
            new()
            {
                Header = "自动化测试",
                Icon = new PathIcon
                {
                    Data = Geometry.Parse(
                        "m437-398 227-226-57-57-170 170-85-85-56 56 141 142ZM40-120v-80h880v80H40Zm120-120q-33 0-56.5-23.5T80-320v-440q0-33 23.5-56.5T160-840h640q33 0 56.5 23.5T880-760v440q0 33-23.5 56.5T800-240H160Zm0-80h640v-440H160v440Zm0 0v-440 440Z"),
                    Width = 24,
                    Height = 24
                },

                PageContent = autoTestPageViewModel
            },
            new()
            {
                Header = "电池内阻测试",
                Icon = new PathIcon
                {
                    Data = Geometry.Parse(
                        "M490 0h-60a60 60 0 0 0-60 60v360a60 60 0 0 0 60 60h60a60 60 0 0 0 60-60V60a60 60 0 0 0-60-60z"),
                    Width = 24,
                    Height = 24
                },

                PageContent = batteryTestViewModel
            },
            new()
            {
                Header = "数据解析配置",
                Icon = new PathIcon
                {
                    Data = Geometry.Parse(
                        "M490 0h-60a60 60 0 0 0-60 60v360a60 60 0 0 0 60 60h60a60 60 0 0 0 60-60V60a60 60 0 0 0-60-60z"),
                    Width = 24,
                    Height = 24
                },

                PageContent = modbusConfigViewModel
            }
        };

        SelectedMenuItem = MenuItems.FirstOrDefault();
    }


    // partial void OnSelectedMenuItemChanged(SukiSideMenuItem value)
    // {
    //     // 通知前一个页面即将离开
    //     if (SelectedMenuItem?.PageContent is IPageLifecycle previousPage)
    //     {
    //         previousPage.OnPageLeaving();
    //     }
    //
    //     // 通知新页面即将进入
    //     if (value?.PageContent is IPageLifecycle newPage)
    //     {
    //         newPage.OnPageEntering();
    //     }
    // }

    /// <summary>
    /// 主题切换
    /// </summary>
    public void ChangeTheme()
    {
        SukiTheme.GetInstance().SwitchBaseTheme();


        // 切换主题后，发送主题变更消息
        // WeakReferenceMessenger.Default.Send(new ThemeChangedMessage(IsDarkTheme));
    }
}